/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Serveur;

import exception.CommunicationException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import librairie.InterfaceTransport;
import librairie.Transport;

/**
 *
 * @author Florian
 */
public class LoanServeur {

    // The default port number that this server will listen on
    private final static int DEFAULT_PORT_NUMBER = 1234;
    // The maximum connections the operating system should
    // accept when the server is not waiting to accept one.
    private final static int MAX_BACKLOG = 20;
    // Timeout in milliseconds for accepting connections.
    // It may go this long before noticing a request to shut down.
    private final static int TIMEOUT = 500;
    // The port number to listen for connections on
    private int portNumber;
    // Sets to true when server should shut down.
    private boolean shutDownFlag = false;
    private int activeConnectionCount = 0;
    
    private ServerSocket s;

    public LoanServeur() {
        portNumber = DEFAULT_PORT_NUMBER;
    }

    public void runServeur() {
        try {
            // Create the ServerSocket.
            System.out.println("Lancement du serveur");
            s = new ServerSocket(portNumber, MAX_BACKLOG);

            // Set timeout for accepting connections so server won't
            // wait forever until noticing a request to shut down.
            s.setSoTimeout(TIMEOUT);
        } catch (IOException e) {
            System.err.println("Impossible de créer la socket");
            e.printStackTrace();
            return;
        } // try

        // loop to keep accepting new connections and talking to clients
        try {
            Socket socket;
          serverLoop:
            while (true) {                  // Keep accepting connections.
                try {
                    socket = s.accept(); // Accept a connection.
                } catch (java.io.InterruptedIOException e) {
                    socket = null;
                    if (!shutDownFlag)
                      continue serverLoop;
                } // try
                if (shutDownFlag) {
                    if (socket != null)
                      socket.close();
                    s.close();
                    return;
                } // if
                
                // Create worker object to process connection.
                System.out.println ("Acceptation d'un client ");
                
                //Création du transport serveur
                InterfaceTransport t = new Transport(socket);
                
                //Création du protocole serveur
                InterfaceProtocoleServeur ps = new ProtocoleServeur(t);
                
                //Création de l'application coté serveur
                ApplicationServeur as = new ApplicationServeur(ps);
            } // while
        } catch (IOException e) {
            // if there is an I/O error just return
        }
    }
}